﻿/** 
 * Copyright 2010 NUMKIT.COM (contact: opensource@numkit.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author 陈超
 * @version 1.0
 */
class Data {
	public var aPiece:Array;
	public var valueTotal:Number;
	public function Data(node:XMLNode) {
		if (node.nodeName != "data") {
			throw new Error("Not A data Node!");
		}
		aPiece = new Array();
		valueTotal = 0;
		var nodeName:String;
		for (var child:XMLNode = node.firstChild; child != null; child=child.nextSibling) {
			nodeName = child.nodeName;
			if (nodeName == "piece") {
				var id:String = child.attributes["id"];
				var color:Number = Number(child.attributes["color"]);
				if (color == undefined || isNaN(color)) {
					throw new Error("Can't find color attribute or invalid!");
				}
				var name:String = undefined;
				var value:Number = undefined;
				for (var grandson:XMLNode = child.firstChild; grandson != null; grandson=grandson.nextSibling) {
					nodeName = grandson.nodeName;
					if (nodeName == "name") {
						name = grandson.firstChild.nodeValue;
					} else if (nodeName == "value") {
						value = Number(grandson.firstChild.nodeValue);
					}
				}
				if (name == undefined) {
					throw new Error("Can't find name node or invalid!");
				}
				aPiece.push(new Piece(id, name, color, value));
				valueTotal += value;
			}
		}
		for(var i=0;i<aPiece.length;i++){
			aPiece[i].initPercent(valueTotal);
		}
	}
}
